﻿using UnityEngine;
using System.Collections;

public class DropSurface : MonoBehaviour {
	
	public GameObject dragItemsContainer;
	
	public void OnDrop(GameObject dropped)
	{
		print ("onPressed :" + dropped);
		DragItem dragItem = dropped.GetComponent<DragItem>();
		
		RecreateDragItem();
		
		if( dragItem == null)
		{
			return;
		}
		
		GameObject newPower = NGUITools.AddChild(this.gameObject, dragItem.CreateOnDrop as GameObject);
		
		GameManager.SetPower(newPower.GetComponent<Power>().type);
		
		Destroy(dropped);
	}
	
	void RecreateDragItem()
	{
		if(GameManager.SelectedPower != Power.Type.None)
		{
			Power selectedPowerScript = transform.GetChild(0).GetComponent<Power>();
			
			NGUITools.AddChild(dragItemsContainer, selectedPowerScript.createOnDestroy as GameObject);
			
			Destroy(selectedPowerScript.gameObject);
		}
	}
	
	void OnClick()
	{
		RecreateDragItem();
		GameManager.SetPower(Power.Type.None);
		
		dragItemsContainer.GetComponent<UIGrid>().Reposition();
			
	}

}
